<?php
class Tag extends Zend_Db_Table{
	protected $_name ="tag";
	protected $_primary = 'tag_id';

    public function getTopTags(){
		$select = $this->select();
		$select->limit('10');
		$select->order('tag_id desc');
		return $this->fetchAll($select)->toArray();
    }
	//根据tag_id 查询tag
    public function gettagBytag_id($tag_id){
		$select = $this->select();
		$select->where("tag_id=".$tag_id);
		return $this->fetchAll($select)->toArray();
    }
//取得所有的标签
    public function getAllTags($page,$perpage,$search_name,$search_key){
          $where = "1=1";
          if($search_name!=""){
              $where.= " and name like '%$search_name%'";
          }
          if($search_key!=""){
              $search_key = explode(' ',$search_key);
              $where .=" and (";
              $count = count($search_key);
              for($i=0;$i<$count;$i++){
                  $where.= " key_word like '%$search_key[$i]%'";
                  if($i!=$count-1){
                      $where.=" or ";
                  }
              }
              $where .= " )";

          }
          $select = $this->select();
          $select->where($where);
          if($page!=""){
          $select->limitPage($page,$perpage);
          }
          $select->order('create_time desc');
         // return $select->__toString();
          return $this->fetchAll($select)->toArray();
    }


 //更新tag
    public function updatetagBytag_id($array,$tag_id){

		$db = $this->getAdapter();
		$where=$db->quoteInto('tag_id = ?', $tag_id);
		$this->update($array, $where);
	}
//删除tag
     public function deltagBytag_id($tag_id){
          $where = 'tag_id = '. $tag_id;
         $this->delete($where);
         return true;
    }
    public  function getAllTagInArray($array){
		$where=" tag_id in (".implode(',',$array).")";
		$select = $this->select();
		$select->where($where);
		$select->order('create_time desc');
		$sql = $select->__toString();
		return $this->fetchAll($select)->toArray();
    }
    
}

